package finedev.work.vennue;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class VideoListAdapter extends ArrayAdapter<VideoItem> {
	
	private Context mContext = null;
	private int mViewResourceId = 0;
	private ArrayList<VideoItem> mContentList = null;
	private LayoutInflater mInflater;
	
	public VideoListAdapter(Context context, int textViewResourceId, ArrayList<VideoItem> contentList) 
	{
		super(context, textViewResourceId, contentList);
		
		mContext = context; // 생성자 초기화 과정
		mViewResourceId = textViewResourceId;
		mContentList = contentList;
		
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
	}

	@Override
	public int getCount() {
		return mContentList.size();
	}

	@Override
	public VideoItem getItem(int position) {
		return mContentList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public View getView(int position, View convertView, ViewGroup parent)
	{
		VideoItem contentItem = mContentList.get(position);
		VideoViewHolder viewHolder;
		
		if ( convertView == null ) 
		{
			convertView = mInflater.inflate(mViewResourceId, null);
			
			viewHolder = new VideoViewHolder();
			
			viewHolder.previewImage = (ImageView) convertView.findViewById(R.id.video_imageview_preview);
			viewHolder.title = (TextView) convertView.findViewById(R.id.video_textview_title);
			viewHolder.postingcount = (TextView) convertView.findViewById(R.id.video_textview_postingcount);
			viewHolder.clickcount = (TextView) convertView.findViewById(R.id.video_textview_clickcount);
			
		}
		else
		{
			viewHolder = (VideoViewHolder) convertView.getTag();
		}
		
		try {
			URL url = new URL( contentItem.getImageUri() );
			Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
			viewHolder.previewImage.setImageBitmap(bitmap);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	    viewHolder.title.setText(contentItem.getTitle());
	    viewHolder.postingcount.setText(contentItem.getPostingCount());
	    viewHolder.clickcount.setText(contentItem.getClickCount());

		return convertView;
		
	}

}
class VideoViewHolder
{
	ImageView previewImage;
	TextView title;
	TextView postingcount;
	TextView clickcount;
}